package com.thinksns.tschat.bean;

import android.content.ContentValues;
import android.os.Parcel;
import android.os.Parcelable;

import com.thinksns.tschat.chat.TSChatManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 类说明 消息列表Modle
 *
 * @version 1.0
 * @date 2015-8-4
 */
public class ModelChatUserList extends Entity implements Parcelable {
    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    private String remark;
    private int room_id;
    private int master_uid;
    private int is_group = 0;    //数据返回为true和false，用整型代替boolean存入数据库，0代表true，1代表false
    private String title;
    private int mtime;
    int self_index;
    private String content;
    private String type;
    //最后一条消息内容
    private int from_uid;
    private String from_uname;
    private String from_uface;
    private String from_uface_url;
    private int lastMsgId;        //最后一条消息ID
    private ModelChatMessage lastMessage;        //最后一条消息
    private int member_num;
    private List<ModelMemberList> memList;
    private String to_name;
    private int to_uid;
    private int isNew;
    private String groupFace;    //群头像
    private int logoId;            //群头像id
    private String groupId;        //创建群房间时加入成员的id

    public ModelChatMessage getLastMessage() {
        return lastMessage;
    }

    public void setLastMessage(ModelChatMessage lastMessage) {
        this.lastMessage = lastMessage;
    }

    public int getLastMsgId() {
        return lastMsgId;
    }

    public void setLastMsgId(int lastMsgId) {
        this.lastMsgId = lastMsgId;
    }

    public String getGroupId() {
        return groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    public List<ModelMemberList> getMemList() {
        return memList;
    }

    public void setMemList(List<ModelMemberList> memList) {
        this.memList = memList;
    }

    public int getRoom_id() {
        return room_id;
    }

    public void setRoom_id(int room_id) {
        this.room_id = room_id;
    }

    public int getMaster_uid() {
        return master_uid;
    }

    public void setMaster_uid(int master_uid) {
        this.master_uid = master_uid;
    }

    public int getIs_group() {
        return is_group;
    }

    public void setIs_group(int is_group) {
        this.is_group = is_group;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getMtime() {
        return mtime;
    }

    public void setMtime(int mtime) {
        this.mtime = mtime;
    }

    public int getSelf_index() {
        return self_index;
    }

    public void setSelf_index(int self_index) {
        this.self_index = self_index;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getFrom_uid() {
        return from_uid;
    }

    public void setFrom_uid(int from_uid) {
        this.from_uid = from_uid;
    }

    public String getFrom_uname() {
        return from_uname;
    }

    public void setFrom_uname(String from_uname) {
        this.from_uname = from_uname;
    }

    public String getFrom_uface() {
        return from_uface;
    }

    public void setFrom_uface(String from_uface) {
        this.from_uface = from_uface;
    }

    public String getFrom_uface_url() {
        return from_uface_url;
    }

    public void setFrom_uface_url(String from_uface_url) {
        this.from_uface_url = from_uface_url;
    }

    public int getMember_num() {
        return member_num;
    }

    public void setMember_num(int member_num) {
        this.member_num = member_num;
    }

    public String getTo_name() {
        return to_name;
    }

    public void setTo_name(String to_name) {
        this.to_name = to_name;
    }

    public int getTo_uid() {
        return to_uid;
    }

    public void setTo_uid(int to_uid) {
        this.to_uid = to_uid;
    }

    public int getIsNew() {
        return isNew;
    }

    public void setIsNew(int isNew) {
        this.isNew = isNew;
    }

    public int getLogoId() {
        return logoId;
    }

    public void setLogoId(int logoId) {
        this.logoId = logoId;
    }

    public String getGroupFace() {
        if (groupFace == null || groupFace.equals("null") || groupFace.isEmpty())
            return null;
        return groupFace;
    }

    public void setGroupFace(String groupFace) {
        this.groupFace = groupFace;
    }

    public ModelChatUserList() {

    }

    public ModelChatUserList(JSONObject object) {
        setData(object);
    }

    public void setData(JSONObject object) {
        try {
            int room_id = object.getInt("room_id");
            int master_uid = object.getInt("master_uid");
            int logo = object.getInt("logo");
            boolean is_group = object.getBoolean("is_group");
            String title = object.getString("title");        //群名称
            int mtime = object.getInt("mtime");                //房间内最后一条消息的时间
            int self_index = object.optInt("self_index");
            int member_num = object.getInt("member_num");
            if (object.has("remark"))
                setRemark(object.getString("remark"));

            lastMessage = new ModelChatMessage();
            //最后一条消息
            String tmpStr = object.get("last_message").toString();
            String content = "";
            if (!tmpStr.equals("[]")) {
                JSONObject lastMsg = object.optJSONObject("last_message");
                if (lastMsg.has("content"))
                    content = lastMsg.optString("content");
                int lastId = lastMsg.getInt("message_id");
                String type = lastMsg.optString("type");        //消息类型
                int from_uid = lastMsg.optInt("from_uid");
                String from_uname = lastMsg.optString("from_uname");

                lastMessage.setMessage_id(lastId);
                lastMessage.setFrom_uid(from_uid);
                lastMessage.setFrom_uname(from_uname);
                lastMessage.setContent(content);
                lastMessage.setType(type);
                if (lastMsg.has("remark"))
                    lastMessage.setRemark(lastMsg.getString("remark"));
                this.lastMsgId = lastId;

            }

            lastMessage.setSendState(ModelChatMessage.SEND_STATE.SEND_OK);
            lastMessage.setMtime(mtime);

            JSONArray memberArray = object.getJSONArray("member_list");
            memList = new ArrayList<ModelMemberList>();
            for (int i = 0; i < memberArray.length(); i++) {
                ModelMemberList member = new ModelMemberList(memberArray.getJSONObject(i));
                memList.add(member);
            }
            this.setRoom_id(room_id);
            //如果是群组，设置群主ID
            this.setMaster_uid(master_uid);
            if (is_group) {
                this.setIs_group(0);
            } else {
                this.setIs_group(1);
            }
            //设置群标题
            this.setTitle(title);
            this.setMtime(mtime);
            this.setSelf_index(self_index);
            this.setMember_num(member_num);
            this.setType(type);
            //设置最后一条消息内容
            this.setContent(content);
            this.setFrom_uid(from_uid);
            this.setFrom_uname(from_uname);

            this.setMemList(memList);
            //设置群头像
            this.setLogoId(logo);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public ContentValues toContentValues() {
        ContentValues map = new ContentValues();
        map.put("login_uid", TSChatManager.getLoginUser().getUid());
        map.put("room_id", getRoom_id());
        map.put("remark", getRemark());
        map.put("master_uid", getMaster_uid());
        map.put("is_group", getIs_group());
        map.put("title", getTitle());
        map.put("mtime", getMtime());
        map.put("self_index", getSelf_index());
        map.put("content", getContent());
        map.put("type", getType());
        map.put("from_uid", getFrom_uid());
        map.put("from_uname", getFrom_uname());
//		map.put("from_uface", getFrom_uface());
//		map.put("from_uface_url", getFrom_uface_url());
//		map.put("to_name", getTo_name());
        map.put("member_num", getMember_num());

        return map;
    }

    // 插入房间列表数据
    public ContentValues insertChatListValues() {

        ContentValues map = new ContentValues();
        map.put("login_uid", TSChatManager.getLoginUser().getUid());
        map.put("room_id", getRoom_id());
        map.put("remark", getRemark());
        map.put("master_uid", getMaster_uid());
        map.put("is_group", getIs_group());
        if (getTitle() != null)
            map.put("title", getTitle());
        map.put("mtime", getMtime());
        map.put("self_index", getSelf_index());
        map.put("content", getContent());
        map.put("type", getType());
        map.put("from_uid", getFrom_uid());
        map.put("from_uname", getFrom_uname());
        map.put("from_uface", getFrom_uface());        //本地头像地址
        map.put("from_uface_url", getFrom_uface_url());
        map.put("to_name", getTo_name());
        map.put("to_uid", getTo_uid());
        map.put("member_num", getMember_num());
        map.put("isNew", getIsNew());
        map.put("group_face", getGroupFace());
        map.put("logo_id", getLogoId());

        return map;
    }

    // 插入房间列表数据
    public ContentValues insertRoomListValues() {
        ContentValues map = new ContentValues();
        map.put("login_uid", TSChatManager.getLoginUser().getUid());
        map.put("room_id", getRoom_id());
        map.put("remark", getRemark());
        map.put("mtime", getMtime());
        map.put("content", getContent());
        map.put("isNew", getIsNew());
        map.put("title", getTitle());
        return map;
    }

    //是否单聊
    public boolean isSingle() {
        return is_group == 1 ? true : false;
    }

    @Override
    public boolean equals(Object o) {
        if (o == null || !(o instanceof ModelChatUserList)) {
            return false;
        }
        return ((ModelChatUserList) o).getRoom_id() == this.getRoom_id();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.remark);
        dest.writeInt(this.room_id);
        dest.writeInt(this.master_uid);
        dest.writeInt(this.is_group);
        dest.writeString(this.title);
        dest.writeInt(this.mtime);
        dest.writeInt(this.self_index);
        dest.writeString(this.content);
        dest.writeString(this.type);
        dest.writeInt(this.from_uid);
        dest.writeString(this.from_uname);
        dest.writeString(this.from_uface);
        dest.writeString(this.from_uface_url);
        dest.writeInt(this.lastMsgId);
        dest.writeSerializable(this.lastMessage);
        dest.writeInt(this.member_num);
        dest.writeList(this.memList);
        dest.writeString(this.to_name);
        dest.writeInt(this.to_uid);
        dest.writeInt(this.isNew);
        dest.writeString(this.groupFace);
        dest.writeInt(this.logoId);
        dest.writeString(this.groupId);
    }

    protected ModelChatUserList(Parcel in) {
        this.remark = in.readString();
        this.room_id = in.readInt();
        this.master_uid = in.readInt();
        this.is_group = in.readInt();
        this.title = in.readString();
        this.mtime = in.readInt();
        this.self_index = in.readInt();
        this.content = in.readString();
        this.type = in.readString();
        this.from_uid = in.readInt();
        this.from_uname = in.readString();
        this.from_uface = in.readString();
        this.from_uface_url = in.readString();
        this.lastMsgId = in.readInt();
        this.lastMessage = (ModelChatMessage) in.readSerializable();
        this.member_num = in.readInt();
        this.memList = new ArrayList<ModelMemberList>();
        in.readList(this.memList, ModelMemberList.class.getClassLoader());
        this.to_name = in.readString();
        this.to_uid = in.readInt();
        this.isNew = in.readInt();
        this.groupFace = in.readString();
        this.logoId = in.readInt();
        this.groupId = in.readString();
    }

    public static final Creator<ModelChatUserList> CREATOR = new Creator<ModelChatUserList>() {
        @Override
        public ModelChatUserList createFromParcel(Parcel source) {
            return new ModelChatUserList(source);
        }

        @Override
        public ModelChatUserList[] newArray(int size) {
            return new ModelChatUserList[size];
        }
    };
}
